/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.gl;

import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import src.domain.gl.Glaccnt;
import src.services.CRUEDService;

/**
 *
 * @author jasuke
 */
public class GlaccntConverter implements Converter{
    
    public static List<Glaccnt> glaccountDB;

    static {
        String sql = 
                "from Glaccnt where glaccntId not in ("+
                "select distinct parentGlaccntId from Glaccnt where parentGlaccntId is not null) "+
                " and suspended <> 1 order by accountNo";
         glaccountDB = new CRUEDService().findAllSQL(sql, null);
    }
        
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        
        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            try {
                int number = Integer.parseInt(submittedValue);
                for (Glaccnt gl : glaccountDB) {
                    if (gl.getGlaccntId() == number) {
                        return gl;
                    }
                }

            } catch(NumberFormatException exception) {
               // throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid glaccount"));
            }
        }
        
        return null;
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
         if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Glaccnt) value).getGlaccntId());
        }
    }
    
   
    
}
